<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../layui/css/layui.css" media="all">
<title>分店零售</title>
</head>
<style>
#addWholesale{
    	position:absolute;
    	top:0%;
    	z-index: 1000;
    	background: rgb(0,0,0,0.9);
    	width:100%;
    	height: 150%;
    	display: none;
}
#showPch{
    	position: absolute;
    	left: 30%;
    	top: 28%;
    	width: 45%;
    	background: white;
    	
}
</style>
<body>
	<br/>
	<div class="demoTable">
		<span style="font-size:16px;">&nbsp;&nbsp;&nbsp;&nbsp;输入会员号码:</span>
		<div class="layui-inline">
		    <input class="layui-input" onMouseOut="yz(this.value)" name="dhhm" id="dhhm" autocomplete="off" placeholder="输入电话号码" maxlength="11">
		</div>
		<button class="layui-btn" data-type="getCheckData" id="add" data-method="confirmTrans">查询</button>
	</div>
	<br/>
	<div class="layui-inline">
		<table class="layui-table" id="fdpf">
			<colgroup>
		      <col width="150">
		      <col width="120">
		      <col width="120">
		      <col width="120">
		      <col width="120">
		      <col width="100">
		      <col width="120">
		      <col width="150">
		      <col width="100">
		      <col width="100">
		    </colgroup>
		    <thead>
				<tr>
			  		<th>药品名称</th>
			  		<th>条形码</th>
			  		<th>药品单位</th>
			  		<th>零售价</th>
			  		<th>零售数量</th>
			  		<th>应收金额</th>
			  		<th>会员折扣率</th>
			  		<th>会员折扣后单价</th>
			  		<th>会员金额</th>
			  		<th>操作</th>
			  	</tr>
		  	</thead>
		  	<tfoot  >
		  		<tr >
			  		<td></td>
			  		<td></td>
			  		<td></td>
			  		<td></td>
			  		<td style="font-size: 23px">总数：</td>
			  		<td id="sum"></td>
			  		<td style="font-size: 23px">合计：</td>
			  		<td id="total"></td>
			  		<td><button class="layui-btn" data-type="getCheckData" onclick="js(this)" data-method="confirmTrans">结算</button></td>
			  		<td></td>
			  	</tr>
		  	</tfoot>
		</table>
	</div>
	<P>&nbsp;</P>
	<P>&nbsp;</P>
	<div class="demoTable">
		  搜索:
	  <div class="layui-inline">
	    <input class="layui-input" name="drugName" id="drugName" autocomplete="off" placeholder="输入药品关键字">
	  </div>
	  <div class="layui-inline layui-form">
			<select name="dtId" id="dtId"
				autocomplete="off" class="layui-input">
				<option></option>
			</select>
	  </div>
	  <div class="layui-inline">
	    <input class="layui-input" name="drugBarCode" id="drugBarCode" autocomplete="off" placeholder="输入药品编码">
	  </div>
	  <div class="layui-inline">
	    <input class="layui-input" name="drugPinyin" id="drugPinyin" autocomplete="off" placeholder="输入药品简称">
	  </div>
      <div class="layui-input-inline" style="width: 100px;">
        <input type="number" id="priceMin" name="priceMin" onkeyup="priceMin()" placeholder="￥" autocomplete="off" class="layui-input">
      </div>
      -
      <div class="layui-input-inline" style="width: 100px;">
        <input type="number" id="priceMax" name="priceMax" onkeyup="priceMax()"   placeholder="￥" autocomplete="off" class="layui-input">
      </div>
		  <button class="layui-btn" data-type="reload">搜索</button>
	</div>
	<table class="layui-hide" id="DrugModel" lay-filter="demo"></table>
	<div id="addWholesale">
		<div id="showPch">
			<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
			  <legend>药品批次及数量选择</legend>
			</fieldset>
			<form class="layui-form" id="addForms" onsubmit="return false" method="post" >
			
			  <div class="layui-form-item">
			    <label class="layui-form-label">药品名称</label>
			    <div class="layui-input-block">
			      <input type="hidden" name="pwId" id="pwIds">
			      <input type="text" name="drugName" id="drugNames" disabled="disabled" maxlength="20" lay-verify="required"  autocomplete="off" class="layui-input" style="width:230px;">
			    </div>
			  </div>
			  <div class="layui-form-item">
			    <label class="layui-form-label">药品批次号</label>
			    <div class="layui-input-block" style="width:230px;">
			      <select name="numbers" id="numbers" lay-filter="numbers" lay-verify="required" lay-search="">
			          
			       </select>
			    </div>
			  </div>
			  <div class="layui-form-item layui-form-text">
			    <label class="layui-form-label" >该批次库存数量</label>
			    <div class="layui-input-block">
			      <input placeholder="库存数量" disabled="disabled" maxlength="18" name="kcNumber" id="kcNumber" class="layui-input"  style="width:230px;"/>
			    </div>
			  </div>
			  <div class="layui-form-item layui-form-text">
			    <label class="layui-form-label" >零售数量</label>
			    <div class="layui-input-block">
			      <input placeholder="数量" onkeyup="this.value=this.value.replace(/[^\d]/g,'') " onafterpaste="this.value=this.value.replace(/[^\d]/g,'') " maxlength="20" name="numbersa" id="cgNumber" class="layui-input" lay-verify="required" style="width:230px;" />
			    </div>
			  </div>
			  <div class="layui-form-item">
			    <div class="layui-input-block">
			      <input type="submit" id="addWhole" class="layui-btn " lay-submit="addWhole" lay-filter="addWhole" value="提交">
			      <button id="closeAdd" type="reset" class="layui-btn layui-btn-primary" >取消</button>
			    </div>
			  </div>
		 </form>
		</div>
	</div>
 <script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-xs " lay-event="edit" id="imgUpdate">加入零售单</a>
</script>

<script type="text/javascript" src="../../res/js/base.js"></script>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="../../layui/layui.js"></script>
<script>
	//输入的电话号码判断
	var st="";
	//结算判断
	var jiesuan="";
	function yz(v){
		var a =/^((([0-9]{4}|[0-9]{3})-)|(\(([0-9]{4}|[0-9]{3})\)))*([0-9]{7}|[0-9]{8})$|^[0-9]{11}$/;
		if(v.length!=11||!v.match(a)){
			st='账号错误';
		}else{
			st="正确";
		};
	}

	//结算
	function js(obj){
		//设置一个数组
		var arr = [];
		//得到goods表格的数据
		var number=0;
		 $("#fdpf tr").each(function(){
			 if(number>1){
				 var row = "";
				 var td = $(this).find("td");
				 row={"drugId":$(td[0]).text(),"drugName":$(td[1]).text(),"numbers":$(td[2]).text(),"mxDiscountRate":$(td[7]).text(),"mxQuantity":$(td[5]).text(),"mxMoney":$(td[9]).text(),"mxMoneys":$(td[6]).text(),"mxPrice":$(td[4]).text(),"mxPrices":$(td[8]).text()};
				 //将数据加入进去
				 arr.push(row);
			 }
			 number=number+1;
		 });
	  	//转换json数据
		 var arrs = JSON.stringify(arr);
	  	if(arrs=="[]"){
	  		layer.msg('亲，您没有购买任何商品哦!!!', {
				  icon: 5,
				  time: 2000 //1秒关闭（如果不配置，默认是3秒）
				}, function(){
				  
			});
	  	}else{
	  		if(jiesuan=="0"){
	  			layer.msg('亲，您输入的会员号码是错误的哦!!!', {
					  icon: 5,
					  time: 1500 //1秒关闭（如果不配置，默认是3秒）
					}, function(){
					  
				});
			}else if(jiesuan=="1"){
				var dhhm = $("#dhhm").val();
				 var url="../yuanwei/fukuan.html?arr="+arrs+"&memberPhone="+dhhm;
				 var url = encodeURI(url);
				layer.open({
					  type: 2,
					  area : ['40%' , '60%'],//弹出框大小
					  shift:1,//弹出框动画效果
					  content: [url,'on'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
					});
			}else{
				 var url="../yuanwei/ptfukuan.html?arr="+arrs;
				 var url = encodeURI(url);
				layer.open({
					  type: 2,
					  area : ['40%' , '60%'],//弹出框大小
					  shift:1,//弹出框动画效果
					  content: [url,'on'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
					});
			}
	  	}
		
	}

var path=getRootPath();
layui.use(['form','laydate', 'laypage', 'layer', 'table', 'upload', 'element'], function(){
	  var laydate = layui.laydate //日期
	  ,laypage = layui.laypage //分页
	  ,layer = layui.layer //弹层
	  ,table = layui.table //表格
	  ,upload = layui.upload //上传
	  ,element = layui.element //元素操作
	 ,form = layui.form;
	  
	  //监听Tab切换
	  element.on('tab(demo)', function(data){
	    layer.msg('切换了：'+ this.innerHTML);
	    console.log(data);
	  });
	  
	  //执行一个 table 实例
	  table.render({
	     elem: '#DrugModel'
	    ,cellMinWidth: 80
	    ,url: getRootPath()+'DrugModel/findFilIdAll.action' //数据接口
	    ,page: true //开启分页
	    ,method:'post'
	    ,cols: [[ //表头
	       {type:'checkbox'}
	      ,{field: 'dtName', title: '药品类型', width:200}
	      ,{field: 'drugBarCode', title: '药品编码', width:200}
	      ,{field: 'drugName', title: '药品名称', width:200}
	      ,{field: 'drugPrice', title: '药品价格', width:100}
	      ,{field: 'pov', title: '药品单位', width:100}
	      ,{field: 'drugPrices', title: '建议零售价', width:100}
	      ,{field: 'drugPinyin', title: '药品拼音简称', width:200}
	      ,{field: 'drugImg',title:'药品图片', width:120, templet:'<div><img src="{{ d.drugImg}}"></div>'}
	      ,{fixed: 'right',title: '操作', width: 100, align:'center', toolbar: '#barDemo'}
	    ]]
	  	,id: 'testReload'
	  });
	  
	  var $ = layui.$;
	  //监听工具条
	  table.on('tool(demo)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
	    var data = obj.data //获得当前行数据
	    ,layEvent = obj.event; //获得 lay-event 对应的值
	    if(layEvent === 'edit'){
	      var drugId=data.drugId;
	      var data={drugId:drugId}
	      $("#pwIds").val(drugId);
	      var url=path+"Inventory/findByfilId.action";
	      $.post(url,data,function(data){
	    	  
	    	  if(data==null||data==""){
	    		  layer.msg('亲，库存不够了', {
					  icon: 5,
					  time: 1000 //1秒关闭（如果不配置，默认是3秒）
					}, function(){
					  
				});
	    	  }else{
	    		  
	    		  
	    		  $("#drugNames").val(data[0].itemName);
	    		  $("#numbers").html('<option value="">请选择批次号</option>');
	    		  for(var i=0;i<data.length;i++){
	    			  $("#numbers").append("<option value='"+data[i].invNumber+"' >"+data[i].numbers+"</option>");
	    		  }
	    		  layui.form.render();
	    		  $('#addWholesale').css('display', 'block');
	    	  }
	      });
	    } 
	  });
	  var $ = layui.$;
		$('#add').on('click', function(){
			if(st=="账号错误"){
				jiesuan=0;
				layer.msg('亲，您输入的不是会员号码哦!!!', {
					  icon: 5,
					  time: 2000 //1秒关闭（如果不配置，默认是3秒）
					}, function(){
					  
				});
			}else if(st=="正确"){
				var dhhm = $("#dhhm").val();
				$.ajax({
	                 url:"../../fdSell/sjhm.action",
	                 data:{"memberPhone":dhhm},
	                 type: "post",
	                 dataType:"json",
	                 cache:false,
	                 async: false,
	                 success: function(obj){
	                	 jiesuan=1;
	                	 layer.msg('亲，您输入的是会员号码!!!', {
	   					  icon: 1,
	   					  time: 2000 //1秒关闭（如果不配置，默认是3秒）
	   					}, function(){
	   					  
	   					});
	                 }
	              });
			}else{
				layer.msg('亲，您还没有输入会员号码哦!!!', {
					  icon: 5,
					  time: 2000 //1秒关闭（如果不配置，默认是3秒）
					}, function(){
					  
				});
			}
			
			 
		      
		});
	 
	  //条件查询	  
	  $('.demoTable .layui-btn').on('click', function(){
		  var drugName = $('#drugName');
			 var dtId = $('#dtId');
			 var drugBarCode = $('#drugBarCode');
			 var drugPinyin = $('#drugPinyin');
			 var priceMin = $('#priceMin');
			 var priceMax = $('#priceMax');
			 //执行重载
		     table.reload('testReload', {
		       page: {
		         curr: 1 //重新从第 1 页开始
		       }
		       ,where: {
		    	   drugName: drugName.val(),
		    	   dtId:dtId.val(),
		    	   drugBarCode:drugBarCode.val(),
		    	   drugPinyin:drugPinyin.val(),
		    	   priceMin:priceMin.val(),
		    	   priceMax:priceMax.val()
		       }
		     });
	  });
	  $('#closeAdd').on('click', function() {
			$('#addWholesale').css('display', 'none');
		});
	  
	  form.on('select(numbers)', function(data){
		  
		  var oCar = document.getElementById("fdpf");
		  
		  var tr=oCar.getElementsByClassName("iii");
		  var trs=oCar.getElementsByClassName("pfxqNum");
		  var pc=oCar.getElementsByClassName("pcNumber");
		  
		  //得到tr的长度
		  var len = tr.length;
		  var ai=data.value;
		  var obj=document.getElementById("numbers");
	      for(i=0;i<obj.length;i++){
	         if(obj[i].selected==true){
	        	 var numbers=obj[i].innerText;//关键是通过option对象的innerText属性获取到选项文本
	         }
	      }
		  //for 循环id为goods表格的数据
		  for(i=0;i<len;i++){
			  //得到点击选中药品的名字
			  var sj=tr[i].innerHTML;
			  //得到表格id为goods药品的名字
			  var joo=$("#drugNames").val();
			  var pcs=pc[i].innerHTML;
			  //判断当选中药品的名字和goods表格中药品名字为一致时
			  if(sj==joo&&pcs==numbers){
				  ai=parseInt(data.value)-parseInt(trs[i].innerHTML);
				  //跳出循环
				  break;
			  }
		  }
		  $("#kcNumber").val(ai);
		  
		  form.render();
		});
	  
	  $("#cgNumber").on('input propertychange',function(){
		  var nums=parseInt($("#cgNumber").val());
		  if($("#kcNumber").val()==""){
			  var num=0;
		  }else{
			  var num=parseInt($("#kcNumber").val());
		  }
		  if(num<nums||num==0){
			  nums=num
			  layer.msg('亲，库存不够了', {
				  icon: 5,
				  time: 1000 //1秒关闭（如果不配置，默认是3秒）
				}, function(){
				  
			});
	  	  }
	  	$("#cgNumber").val(nums);
	  	
	  });
	  form.on('submit(addWhole)',function(data){
		  
		  var obj=document.getElementById("numbers");
	      for(i=0;i<obj.length;i++){
	         if(obj[i].selected==true){
	        	 var numbers=obj[i].innerText;//关键是通过option对象的innerText属性获取到选项文本
	         }
	      }
		  var pwId=$("#pwIds").val();
		  var invNumber=$("#cgNumber").val();
		  var data={numbers:numbers,pwId:pwId,invNumber:invNumber};
		  
		  
		  var url=path+"wholesaleDetail/storage.action";
		  
		  $.post(url,data,function(msg){
			  
			  var oCar = document.getElementById("fdpf");
			  
			  var tr=oCar.getElementsByClassName("iii");
			  var tra=oCar.getElementsByClassName("pfxqShub");
			  var trs=oCar.getElementsByClassName("pfxqNum");
			  var trr=oCar.getElementsByClassName("pfhuiyaun");
			  var tre=oCar.getElementsByClassName("lsjg");
			  //得到tr的长度
			  var len = tr.length;
			  var ai=0;
			  //总数量
			  var nums=0;
			  //合计
			  var total=0;
			  //for 循环id为goods表格的数据
			  for(i=0;i<len;i++){
				  //得到点击选中药品的名字
				  var sj=tr[i].innerHTML;
				  //得到表格id为goods药品的名字
				  var joo=msg.drugName;
				  
				  //判断当选中药品的名字和goods表格中药品名字为一致时
				  if(sj==joo){
					  ai=1;
					  var hyzk=msg.pfxqNumber*tre[i].innerHTML*0.95;
					  var hyjg=trr[i].innerHTML
					  var zk=parseFloat(hyzk)+parseFloat(hyjg);
					  trs[i].innerHTML=parseInt(msg.pfxqNumber)+parseInt(trs[i].innerHTML);
					  tra[i].innerHTML=parseInt(msg.pfxqSubtotal)*parseInt(tre[i].innerHTML)+parseInt(tra[i].innerHTML);
					  trr[i].innerHTML=zk;
				  }
				  nums=nums+parseInt(trs[i].innerHTML);
				  total=total+parseInt(tra[i].innerHTML);
			  }
			  if(ai==0){
				  //应收的金额
				  var ysje=msg.fdPrice*msg.pfxqNumber;
				  //会员折扣
				  var zk=0.95;
				  //会员折扣后单价
				  var hydj=msg.fdPrice*zk;
				  hydj = hydj-0 +.00000000000001;
				  hydj*=100;
				  hydj=(Math.floor(hydj)/100).toFixed(2);
				  
				  //会员折扣后总金额
				  var hyje=msg.pfxqNumber*hydj;
				  hyje = hyje-0 +.00000000000001;
				  hyje*=100;
				  hyje=(Math.floor(hyje)/100).toFixed(2);
				  
				  $("#fdpf").append('<tr><td style="display:none">'+msg.pwId+'</td><td class="iii">'+msg.drugName+'</td><td class="pcNumber">'+msg.numbers+'</td><td>'+msg.pfxqUnit+'</td><td class="lsjg">'+msg.fdPrice+'</td><td class="pfxqNum">'+msg.pfxqNumber+'</td><td class="pfxqShub">'+ysje+'</td><td>'+zk+'</td><td>'+hydj+'</td><td class="pfhuiyaun">'+hyje+'</td><td><a href="javascript:;" onclick="show(this)">删除</a></td></tr>');
				  nums=nums+parseInt(msg.pfxqNumber);
				  
				  total=total+parseInt(ysje);
			  }
			  $("#sum").html(nums);
			  $("#total").html(total);
			  $('#addWholesale').css('display', 'none');
			  $("#pwIds").val("");
			  $("#cgNumber").val("");
			  $("#kcNumber").val("");
			  $("#drugNames").val("");
		  });
		  
	  });
	});
function show(obj){
	var oDivs= obj.parentNode.parentNode;
	obj.parentNode.parentNode.parentNode.removeChild(oDivs);
}
</script>
</body>
</html>